<?php

class rank extends bhCommon {

    public function __construct()
    {
        parent::__construct();
    }


    public function today()
    {
        $today = strtotime(date('Y-m-d'));
        $sql = "SELECT B.nickname,B.head,A.currency FROM " . prefix('day') . " AS A LEFT JOIN " . prefix('member') . " AS B ON A.member_id=B.id WHERE A.same_day = {$today} AND A.uniacid={$this->w['uniacid']} ORDER BY currency DESC LIMIT 100";

        $rank = p_fetchall($sql);
        $config = array(
            'currency_name' => getConfig('currency_name', '健康币'),
            'grid_ad' => getad(),
            'ad_type' => getConfig('ad_type', 1),
        );
        $config['ad_show_type'] = $this->rising->get_ad_type($config['ad_type']);
        $data = array(
            'rank' => $rank,
            'config' => $config
        );
        $this->json($data);
    }



    public function all()
    {
        $rank = p_getall('member', array(), array(), '', 'currency desc', 100);
        $data = array(
            'rank' => $rank
        );
        $this->json($data);
    }


    public function popularity()
    {
        $p = $this->get('p', 1);
        $page_size = 4;
        $limit = ($p - 1) * $page_size;

        $user = p_getall('member', array(), array(), '', 'popularity desc,id asc', 100);



        $data = array('user' => $user);
        $this->json($data);
    }


    public function share()
    {
        $share_id = $this->get('share_id');

        $share_member = array();

        $isPopularitye = $this->rising->isPopularitye();
        $my_rank = 0;
        $ad = array();
        $p_share = array();

        if ($isPopularitye) {
            if ($share_id && $share_member != $this->member['id']) {
                $share_member = p_get('member', array('id' => $share_id));

                $sql = "SELECT COUNT(*) FROM " . prefix('member') . " WHERE popularity>{$share_member['popularity']} OR (id < {$share_member['id']} AND popularity = {$share_member['popularity']}) " . get_uniacid();
                $share_member['rank'] = intval(p_fetchcolumn($sql)) + 1;
            }

            $sql = "SELECT COUNT(*) FROM " . prefix('member') . " WHERE popularity>{$this->member['popularity']} OR (id < {$this->member['id']} AND popularity = {$this->member['popularity']})" . get_uniacid();
            $my_rank = intval(p_fetchcolumn($sql)) + 1;
            $ad = $this->rising->getAdvertisement(1);

            $p_share = array(
                'text' => str_replace('{name}', $this->member['nickname'], getConfig('popularity_share_text')),
                'images' => getImage(getConfig('popularity_share_images'))
            );
        }


        $data = array(
            'share_member' => $share_member,
            'video_ad' => getConfig('video_ad'),
            'my_rank' => $my_rank,
            'member' => $this->member,
            'isPopularitye' => $isPopularitye,
            'ad' => $ad,
            'p_share' => $p_share
        );

        $this->json($data);
    }



    public function guard()
    {
        $guard_popularity = getConfig('guard_popularity', 10);

        if ($guard_popularity > 0) {
            $member_id = $this->get('member_id');
            if ($this->rising->change_popularity($member_id, $guard_popularity, 1, $this->member['id'])) {
                //$this->json(array('guard_popularity' => $guard_popularity));
            }
        }

        $this->json(array('guard_popularity' => $guard_popularity));
    }


}